package com.syni.mdd.yhd.common.all.service.impl.groupBuy;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.dao.jpa.groupBuy.BmsDxGroupBuyRecordDao;
import com.syni.mdd.yhd.common.all.dao.jpa.groupBuy.BmsDxSaleDao;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrder;
import com.syni.mdd.yhd.common.all.entity.groupBuy.BmsDxGroupBuyRecord;
import com.syni.mdd.yhd.common.all.entity.groupBuy.BmsDxSale;
import com.syni.mdd.yhd.common.all.service.groupBuy.BmsDxGroupBuyRecordService;
import com.syni.mdd.yhd.common.all.service.groupBuy.BmsDxSaleService;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsDxSaleServiceImpl extends BaseService<BmsDxSale,Integer> implements BmsDxSaleService {

    @Autowired
    BmsDxSaleDao bmsDxSaleDao;

    @Override
    public BmsDxSaleDao getBaseDao() {
        return bmsDxSaleDao;
    }

    @Override
    public void postHandGroupBuyCountRecommend(BmsDxOrder bmsDxOrder) {
        String remarks = bmsDxOrder.getRemark();
        if(StringUtils.isNotBlank(remarks)){
            Map<String,Object> data = JSON.parseObject(remarks);
            if(data.containsKey("reId") && StringUtils.checkNullAndEmpty(data.get("reId"))){
                Integer reId = Integer.parseInt(data.get("reId").toString());
                Optional<BmsDxSale> optionalBmsDxSale = bmsDxSaleDao.findById(reId);
                if(optionalBmsDxSale.isPresent()){
                    BmsDxSale bmsDxSale = optionalBmsDxSale.get();
                    bmsDxSale.setSaleNum(bmsDxSale.getSaleNum() == null ? 1 : bmsDxSale.getSaleNum() + 1);
                    bmsDxSaleDao.save(bmsDxSale);
                }
            }
        }
    }
}
